package com.itsu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Configuration
public class RedisConfig {

    private final static Integer PORT = 6379;
    private final static String hostName = "192.168.130.2";

    //第一第二对象可不必加入容器中也可正常使用Redis

    //@Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setPort(PORT);
        jedisConnectionFactory.setHostName(hostName);
        return jedisConnectionFactory;
    }

    //@Bean
    public RedisTemplate  redisTemplate(@Autowired JedisConnectionFactory jcf) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jcf);
        return redisTemplate;
    }

    @Bean
    public JedisPool jedisPool() {
        JedisPool jedisPool = new JedisPool(hostName, PORT);
        return jedisPool;
    }
}
